import { createRouter, createWebHistory } from 'vue-router'
import SupplierManagement from '../components/SupplierManagement.vue'
import CustomerManagement from '../components/CustomerManagement.vue'
import EmployeeManagement from '../components/EmployeeManagement.vue'
import QuotationForm from '../components/QuotationForm.vue'
import QuotationRecord from '../components/QuotationRecord.vue'
import ContractAudit from '../components/ContractAudit.vue'
import OutsourcingManagement from '../components/OutsourcingManagement.vue'
import InboundManagement from '../components/InboundManagement.vue'
import InboundRecord from '../components/InboundRecord.vue'
import OutboundManagement from '../components/OutboundManagement.vue'
import OutboundRecord from '../components/OutboundRecord.vue'
import InventoryManagement from '../components/InventoryManagement.vue'
import LabelMaker from '../views/LabelMaker.vue'
import Login from '../views/Login.vue'

// 路由配置
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '登录',
      requiresAuth: false
    }
  },
  {
    path: '/',
    redirect: '/supplier',
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/supplier',
    name: 'Supplier',
    component: SupplierManagement,
    meta: {
      title: '供应商管理',
      requiresAuth: true
    }
  },
  {
    path: '/customer',
    name: 'Customer',
    component: CustomerManagement,
    meta: {
      title: '客户管理',
      requiresAuth: true
    }
  },
  {
    path: '/employee',
    name: 'Employee',
    component: EmployeeManagement,
    meta: {
      title: '员工管理',
      requiresAuth: true
    }
  },
  {
    path: '/quotation',
    name: 'Quotation',
    component: QuotationForm,
    meta: {
      title: '报价单',
      requiresAuth: true
    }
  },
  {
    path: '/quotation-record',
    name: 'QuotationRecord',
    component: QuotationRecord,
    meta: {
      title: '报价记录',
      requiresAuth: true
    }
  },
  {
    path: '/contract-audit',
    name: 'ContractAudit',
    component: ContractAudit,
    meta: {
      title: '合同审核',
      requiresAuth: true
    }
  },
  {
    path: '/outsourcing',
    name: 'Outsourcing',
    component: OutsourcingManagement,
    meta: {
      title: '外发加工',
      requiresAuth: true
    }
  },
  {
    path: '/inbound',
    name: 'Inbound',
    component: InboundManagement,
    meta: {
      title: '入库',
      requiresAuth: true
    }
  },
  {
    path: '/inbound-record',
    name: 'InboundRecord',
    component: InboundRecord,
    meta: {
      title: '入库记录',
      requiresAuth: true
    }
  },
  {
    path: '/outbound',
    name: 'Outbound',
    component: OutboundManagement,
    meta: {
      title: '出库',
      requiresAuth: true
    }
  },
  {
    path: '/outbound-record',
    name: 'OutboundRecord',
    component: OutboundRecord,
    meta: {
      title: '出库记录',
      requiresAuth: true
    }
  },
  {
    path: '/inventory',
    name: 'Inventory',
    component: InventoryManagement,
    meta: {
      title: '库存',
      requiresAuth: true
    }
  },
  {
    path: '/label-maker',
    name: 'LabelMaker',
    component: LabelMaker,
    meta: {
      title: '标签制作',
      requiresAuth: true
    }
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由前置守卫：登录验证和设置页面标题
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - PCB ERP管理系统`
  } else {
    document.title = 'PCB ERP管理系统'
  }
  
  // 检查是否需要登录验证
  if (to.meta.requiresAuth !== false) {
    // 检查是否已登录
    const token = localStorage.getItem('token')
    const userInfo = localStorage.getItem('userInfo')
    
    if (!token || !userInfo) {
      // 未登录，跳转到登录页
      next({ name: 'Login' })
      return
    }
  }
  
  next()
})

export default router 